// pages/resume-pages/education-experience/education-experience.js
const _month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
import Toast from '../../../component/toast/toast';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        education: {},
        majorName: '',
        schoolName: '',
        startDate: '',
        endDate: '',
        pickerDate: [
            [], _month
        ],
        startDateIndexs: [0, 0],
        endDateIndexs: [0, 0],
        flag: 0,
        otherParams:null,
        educationId:''
    },
    //输入学历
    educationPickerChange(e) {
        let that = this;
        this.setData({
            education: that.data.otherParams.EducationData[e.detail.value]
        })
    },
    //输入专业
    inputMajor(e) {
        this.setData({
            majorName: e.detail.value
        });
    },
    //输入学校
    inputSchool(e) {
        this.setData({
            schoolName: e.detail.value
        });
    },
    
    get20Year() {
        let currentYear = parseInt(new Date().getFullYear());
        let year = currentYear;
        let yearsArr = [];
        for (let i = currentYear - 20; i <= currentYear; i++) {
            yearsArr.push(year--);
        }
        return yearsArr;
    },
    // 开始工作日期picker
    startDatePickerChange(e) {
        let pickerDate = this.data.pickerDate;
        let startDateIndexs = this.data.startDateIndexs;
        let startDate = pickerDate[0][startDateIndexs[0]] + "-" + pickerDate[1][startDateIndexs[1]];
        this.setData({
            startDate: startDate
        });
    },
    startDateItemPickerChange(e) {
        let data = {
            startDateIndexs: this.data.startDateIndexs
        }
        if (e.detail.column == 0) {
            data.startDateIndexs[0] = e.detail.value;
        } else if (e.detail.column == 1) {
            data.startDateIndexs[1] = e.detail.value;
        }
        this.setData(data);
    },
    //结束工作日期picker
    endDatePickerChange(e) {
        let pickerDate = this.data.pickerDate;
        let endDateIndexs = this.data.endDateIndexs;
        let endkDate = pickerDate[0][endDateIndexs[0]] + "-" + pickerDate[1][endDateIndexs[1]];
        this.setData({
            endDate: endkDate
        });
    },
    endDateItemPickerChange(e) {
        let data = {
            endDateIndexs: this.data.endDateIndexs
        }
        if (e.detail.column == 0) {
            data.endDateIndexs[0] = e.detail.value;
        } else if (e.detail.column == 1) {
            data.endDateIndexs[1] = e.detail.value;
        }
        this.setData(data);
    },
    clickSave() {
        let education = this.data.education;
        if (!education) {
            Toast("请填写您的学历");
            return;
        }
        let majorName = this.data.majorName;
        if (!majorName) {
            Toast("请填写所在专业");
            return;
        }
        let schoolName = this.data.schoolName;
        if(!schoolName){
            Toast("请填写学校名称");
            return;
        }
        let startDate = this.data.startDate;
        if (!startDate) {
            Toast("请填写开始时间");
            return;
        }
        let endDate = this.data.endDate;
        if (!endDate) {
            Toast("请填写结束时间");
            return;
        }
        let data = {
            Education: education.Id,
            Specialty: majorName,
            SchoolName: schoolName,
            StartDate: startDate,
            EndDate: endDate
        };
        if (this.data.flag == 0) {
            //新增教育经历
            data.Id = app.userInfo.Id;;
            data.ResumeInfoId = app.globalData.resumeInfo.Id;
            this.addEducationExperience(data);
        }
        else if(this.data.flag == 1){
            data.Id = this.data.educationId;
            this.editEducationExperience(data);
        }
    },
    clickdelete(){
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除当前经历吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#4EB262',
            confirmText: '确定',
            confirmColor: '#222',
            success: function (res) {
                if (res.confirm) {
                    that.deleteEducation();
                }
            }
        })
        
    },
    deleteEducation(){
        let data = {
            CurrentUserInfoId: app.userInfo.Id,
            ResumeInfoEducationalExperienceId: this.data.educationId
        }
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.deleteEducationExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                console.log("deleteEducationExperience response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        Toast(res.data.Message);
                    }
                } else {
                    Toast("删除失败，请检查网络！");
                }
            },
            fail: function (res) { console.log("deleteEducationExperience fail", res) },
            complete: function (res) { wx.hideLoading() },
        })
    },
    //新增教育经历
    addEducationExperience(data) {
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.addEducationExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("addEducationExperience response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        Toast(res.data.Message);
                    }
                } else {
                    Toast("保存失败，请检查网络！");
                }
            },
            fail: function(res) {
                console.log("addEducationExperience fail", res)
            },
            complete: function(res) {
                wx.hideLoading()
            },
        })
    },
    //修改教育经历
    editEducationExperience(data){
        wx.showLoading({
            title: '提交中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.editEducationExperience,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                console.log("editEducationExperience response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        Toast(res.data.Message);
                    }
                } else {
                    Toast("保存失败，请检查网络！");
                }
            },
            fail: function (res) {
                console.log("editEducationExperience fail", res)
            },
            complete: function (res) {
                wx.hideLoading()
            },
        })
    },
    getPositionOtherParams() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.getPositionOtherParams,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log("getPositionOtherParams success：", res.data.BodyContent);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    let otherParams = res.data.BodyContent;
                    otherParams.EducationData.splice(0 , 1);
                    that.setData({
                        otherParams: res.data.BodyContent
                    });
                }
            },
            fail: function (res) {
                console.log("getPositionOtherParams fail", res)
            },
            complete: function (res) {
                wx.hideLoading()
            },
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let years = this.get20Year();
        let key = `pickerDate[0]`
        this.setData({
            [key]: years
        });
        this.getPositionOtherParams();
        if (options.parmas){
            let obj = JSON.parse(options.parmas);
            let data = {
                education: {},
                majorName: obj.Specialty,
                schoolName: obj.SchoolName,
                startDate: obj.StartDate.substring(0, obj.StartDate.lastIndexOf('-')),
                endDate: obj.EndDate.substring(0, obj.EndDate.lastIndexOf('-')),
                educationId: obj.Id,
                flag:1
            }
            data.education.Id = obj.Education;
            data.education.ParamName = obj.EducationText;
            this.setData(data);
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})